package com.project.controller;

import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.ShoppingCarBean;
import com.project.bean.UserBean;
import com.project.service.IShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private IShoppingCarService service;

    @RequestMapping("buy")
    public String buy(int productId, HttpServletRequest request){
        //得到登陆用户
        UserBean user = (UserBean)request.getSession().
                getAttribute("loginUser");
        service.add(user.getId(),productId);

        return "ok";
    }

    @RequestMapping("findByUser")
    public List<ShoppingCarBean> findByUser(HttpServletRequest request) throws Exception {
        //得到登陆用户
        UserBean user = (UserBean)request.getSession().
                getAttribute("loginUser");
        List<ShoppingCarBean> list = service.findByUser(user.getId());
        return list;
    }

    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "ok";
    }

    @RequestMapping("update")
    public String update(int id,int num){
        service.update(id,num);
        return "ok";
    }
}
